﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.Serialization;

namespace UlsLogViewer.Entities
{
    [DataContract]
    public class LogEntry
    {
        public LogEntry(IList<string> fields)
        {
            DateTime dt; 
            var provider = CultureInfo.InvariantCulture;
            if (DateTime.TryParseExact(fields[0].Trim(), "MM/dd/yyyy HH:mm:ss.ff", provider, DateTimeStyles.None, out dt))
            {
                Time = dt;
            }
            Process = fields[1];
            Thread = fields[2];
            Product = fields[3];
            Category = fields[4];
            EventID = fields[5];
            Level = fields[6];
            Message = fields[7];
            Correlation = fields[8];
        }

        public int Id { get; set; }

        public DateTime? Time { get; set; }

        private string _dt;
        [DataMember]
        public string TimeStamp
        {
            get { return Time.HasValue ? Time.Value.ToString("dd/MM/yyyy hh:mm:ss.fff") : string.Empty; }
            set { _dt = value; }
        }

        [DataMember]
        public string Process { get; set; }

        [DataMember]
        public string Thread { get; set; }

        [DataMember]
        public string Product { get; set; }

        [DataMember]
        public string Category { get; set; }

        [DataMember]
        public string EventID { get; set; }

        [DataMember]
        public string Level { get; set; }

        [DataMember]
        public string Correlation { get; set; }

        [DataMember]
        public string Message { get; set; }

        public string MessageSnippet 
        { 
            get
            {
                if(!string.IsNullOrEmpty(Message))
                {
                    return Message.Length > 50 ? Message.Substring(0, 50) + "..." : Message;
                }
                return string.Empty;
            }
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            if (ReferenceEquals(this, obj)) return true;
            if (obj.GetType() != typeof (LogEntry)) return false;
            return Equals((LogEntry) obj);
        }

        public bool Equals(LogEntry other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Time.Equals(Time) && Equals(other.Process, Process) && Equals(other.Thread, Thread) && Equals(other.Product, Product) && Equals(other.Category, Category) && Equals(other.EventID, EventID) && Equals(other.Level, Level) && Equals(other.Correlation, Correlation) && Equals(other.Message, Message);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                int result = (Time.HasValue ? Time.Value.GetHashCode() : 0);
                result = (result*397) ^ (Process != null ? Process.GetHashCode() : 0);
                result = (result*397) ^ (Thread != null ? Thread.GetHashCode() : 0);
                result = (result*397) ^ (Product != null ? Product.GetHashCode() : 0);
                result = (result*397) ^ (Category != null ? Category.GetHashCode() : 0);
                result = (result*397) ^ (EventID != null ? EventID.GetHashCode() : 0);
                result = (result*397) ^ (Level != null ? Level.GetHashCode() : 0);
                result = (result*397) ^ (Correlation != null ? Correlation.GetHashCode() : 0);
                result = (result*397) ^ (Message != null ? Message.GetHashCode() : 0);
                return result;
            }
        }
    }
}
